<?php
/*
 * 订单
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\facade\Db;
use kuaidi100\Kuaidi; //快递API
use app\common\model\System;

class Orders extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    //订单列表
    public function lists()
    {
        $param = $this->request->param();
        if (!empty($param['title'])) {
            $where = function ($query) use ($param){$query->whereOr('name|order_sn|consignee|goods_name',trim($param['title']));};
        }else{
           $where = 1;
        }
        $list = Db::table('orders')
                ->alias('o')
                ->leftJoin('order_goods g','g.order_id = o.id')
                ->leftJoin('user u','o.user_id = u.id')
                ->field('o.id,u.name user_name,o.order_sn,o.order_status,g.goods_name,g.goods_price,g.goods_attr,g.goods_number,o.shipping_status,o.pay_status,o.consignee,o.city,o.address,o.mobile,o.postscript,o.order_amount,o.money_paid,o.integral,o.balance,o.pay_name,o.pay_time,o.is_separate,o.status,o.invoice_no')
                ->where($where)->order('pay_time','DESC')->paginate();
        return $this->fetch('index',['list'=>$list]);

    }

    //查看详细
    public function detail()
    {
        $data = Db::table('orders')
                ->alias('o')
                ->leftJoin('order_goods g','g.order_id = o.id')
                ->leftJoin('user u','o.user_id = u.id')
                ->field('o.id,u.name user_name,o.order_sn,o.order_status,g.goods_name,g.goods_price,g.goods_attr,g.goods_number,o.shipping_status,o.pay_status,o.consignee,o.city,o.address,o.mobile,o.postscript,o.order_amount,o.money_paid,o.integral,o.balance,o.pay_name,o.pay_time,o.is_separate,o.status,o.create_time')->where('o.id',trim(input('id')))->find();
        return $this->fetch('detail',['data'=>$data]);
    }

    //发货
    public function send()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $id = trim($param['id']);
            $param['shipping_status'] = 1;
            $result = Db::table('orders')->where(['id'=>$id])->update($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('发货了');
            $this->success('发货成功', url('@index'));
        }
        return $this->fetch('send');
    }

    // 批量发货
    public function batchsend()
    {
        if ($this->request->isPost()) {
            $file = $this->request->file('file');
            $name = $file->getoriginalName();
            $filePath = $file->getRealPath();
            
            
            // insert_admin_log('批量发货');
            $this->success('发货成功');
        }
    }

    //物流追踪
    public function express()
    {
        $exp = System::get_config_by_code('express');
        $num = Db::table('orders')->where('id',input('id'))->value('invoice_no');
        $data = Kuaidi::synquery($num);//物流查询
        return $this->fetch('express',['data'=>$data]);
    }

    //导出未发货订单
    public function export()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $today = strtotime(date('Y-m-d'));
            $start = empty($param['start_time'])?$today:strtotime($param['start_time']);
            $end = empty($param['end_time'])?$today+86399:strtotime($param['end_time']);
            $end < $start && $this->error('时间选择错误，请重新选择！');//结束时间小于开始时间
            $title = ['订单号','快递单号'];
            $data = Db::table('orders')->field('order_sn,invoice_no')
                // ->whereTime('pay_time', 'between', [$start, $end])
                ->whereBetweenTime('pay_time', $start, $end)
                ->where(['order_status'=>1,'pay_status'=>1,'shipping_status'=>0])
                ->order('pay_time','DESC')
                ->select()
                ->toArray();
            empty($data) && $this->error('未查询到订单！');//结束时间小于开始时间
            export_excel($data, $title,date('YmdHis').'未发货订单导出');
            $this->success('导出成功');
        }
        return $this->fetch('export');
    }

    //导出全部订单
    public function exportAll()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $today = strtotime(date('Y-m-d'));
            $start = empty($param['start_time'])?$today:strtotime($param['start_time']);
            $end = empty($param['end_time'])?$today+86399:strtotime($param['end_time']);
            $end < $start && $this->error('时间选择错误，请重新选择！');//结束时间小于开始时间
            $title = ['会员编号','订单号','订单状态','商品名称','单价','规格','数量','发货状态','支付状态','收货人','城市','地址','手机号','留言、备注','应付金额','已付金额','积分支付','余额支付','支付方式','支付时间','分销状态','状态'];
            $data = Db::table('orders')
                ->alias('o')
                ->leftJoin('order_goods g','g.order_id = o.id')
                ->leftJoin('user u','o.user_id = u.id')
                ->field('u.name user_name,o.order_sn,o.order_status,g.goods_name,g.goods_price,g.goods_attr,g.goods_number,o.shipping_status,o.pay_status,o.consignee,o.city,o.address,o.mobile,o.postscript,o.order_amount,o.money_paid,o.integral,o.balance,o.pay_name,o.pay_time,o.is_separate,o.status')
                ->withAttr('status', function($value) {  // 动态获取器
                    $status = ['禁用','正常'];
                    return $status[$value];
                })
                ->withAttr('pay_status', function($value) {  // 动态获取器
                    $is_frozen = ['未支付','已支付'];
                    
                    return $is_frozen[$value];
                })
                ->withAttr('shipping_status', function($value) {  // 动态获取器
                    $is_frozen = ['未发货','已发货'];
                    return $is_frozen[$value];
                })
                ->withAttr('order_status', function($value) {  // 动态获取器
                    $is_active = ['未确认','已确认'];
                    return $is_active[$value];
                })
                ->withAttr('is_separate', function($value) {  // 动态获取器
                    $is_active = ['未分成','已分成','已撤销'];
                    return $is_active[$value];
                })
                ->whereBetweenTime('pay_time', $start, $end)
                ->where(['order_status'=>1,'pay_status'=>1,'shipping_status'=>0])
                ->order('pay_time','DESC')
                ->select()
                ->toArray();
            $this->success('导出成功');
            // export_excel($data, $title,date('YmdHis').'订单导出');
        }
        return $this->fetch('export');
    }

    //删除订单(未启用)
    public function del()
    {
        $this->error('你想干嘛？');
        if ($this->request->isPost()) {
            $param = $this->request->param();
            Db::table('order_goods')->where('order_id',$param['id'])->find() && $this->error('无法删除该订单 ~');
            Db::table('orders')->where('pay_status',1)->find() && $this->error('无法删除已付款订单 ~');
            Db::table('orders')->where('shipping_status',1)->find() && $this->error('无法删除已发货订单 ~');
            Db::table('orders')->delete($param['id']);
            insert_admin_log('删除了订单');
            $this->success('删除成功');
        }
    }



}
